package org.teamh.gui.lobby;

import static org.lwjgl.opengl.GL11.*;

import java.io.File;

import org.teamh.gui.Event;
import org.teamh.gui.GUI;
import org.teamh.gui.Screen;

import de.lessvoid.nifty.builder.ControlBuilder;
import de.lessvoid.nifty.builder.ControlDefinitionBuilder;
import de.lessvoid.nifty.builder.EffectBuilder;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.listbox.builder.ListBoxBuilder;
import de.lessvoid.nifty.controls.scrollpanel.builder.ScrollPanelBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.ImageRenderer;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.tools.Color;


public class LobbyScreen implements Screen {
	
	private GUI gui;
	private LobbyController lobbyController;
	private String fontName;
	
	private File previewImage;
	
	public LobbyScreen(GUI gui) {
		this.gui = gui;
		this.lobbyController = new LobbyController(gui);
	    fontName = gui.getNifty().getRenderEngine().getFontname(gui.getNifty().getRenderEngine().getFont());
	}

	@Override
	public void build() {
	    new ControlDefinitionBuilder("gameBox-element") {{
	    	style("nifty-listbox-item");
	    	controller("de.lessvoid.nifty.controls.listbox.ListBoxItemController");
			panel(new PanelBuilder("#outer_gameBox") {{
				childLayoutHorizontal();
				height("23px");
				text(new TextBuilder("#name") {{
					width("180px");
					font(LobbyScreen.this.getFontName());
				}});
				panel(new PanelBuilder() {{
					width("1px");
				}});
				text(new TextBuilder("#map") {{
					width("180px");
					font(LobbyScreen.this.getFontName());
				}});
				panel(new PanelBuilder() {{
					width("5px");
				}});
				text(new TextBuilder("#player") {{
					width("50px");
					font(LobbyScreen.this.getFontName());
				}});
				panel(new PanelBuilder() {{
					width("5px");
				}});
				text(new TextBuilder("#status") {{
					width("100px");
					font(LobbyScreen.this.getFontName());
				}});
			}});
		}}.registerControlDefintion(gui.getNifty());
		
		de.lessvoid.nifty.screen.Screen screen = new ScreenBuilder("lobby") {{
			controller(lobbyController);
			layer(new LayerBuilder() {{
				childLayoutCenter();
				panel(new PanelBuilder() {{
					onStartScreenEffect(new EffectBuilder("move") {{
						effectParameter("mode", "in");
						effectParameter("direction", "left");
						length(500); 
						startDelay(0);
						inherit(true);
						post(false);
					}});
					onEndScreenEffect(new EffectBuilder("move") {{
						effectParameter("mode", "out");
						effectParameter("direction", "right");
						length(500);
						startDelay(0);
						inherit(true);
					}});
					style("nifty-panel");
					width("1024px");
					height("768px");
					childLayoutVertical();
					panel(new PanelBuilder() {{	
						childLayoutHorizontal();
						panel(new PanelBuilder() {{	
							width("21px");
						}});
						panel(new PanelBuilder() {{	
							childLayoutVertical();
							panel(new PanelBuilder() {{
								height("21px");
							}});
							text(new TextBuilder() {{
								font("aurulent-sans-36-bold-with-shadow.fnt");
								color(new Color(1, 1, 1, 1));
								text("Games");
							}});
							control(new ListBoxBuilder("gamesBox") {{
								style("nifty-listbox");
								displayItems((768 / 64) + 1);
								selectionModeSingle();
								hideHorizontalScrollbar();
								optionalVerticalScrollbar();
								visibleToMouse(true);
								control(new ControlBuilder("gameBox-element") {{
									
								}});
							}});
						}});
						panel(new PanelBuilder() {{	
							width("21px");
						}});
						panel(new PanelBuilder() {{	
							width("389px");
							childLayoutVertical();
							panel(new PanelBuilder() {{
								height("21px");
							}});
							text(new TextBuilder() {{
								font("aurulent-sans-36-bold-with-shadow.fnt");
								color(new Color(1, 1, 1, 1));
								text("Players");
							}});
							panel(new PanelBuilder() {{
								childLayoutHorizontal();
								control(new ScrollPanelBuilder("scroll"){{
									style("nifty-listbox");
									set("horizontal", "false");
									width("*");
									height("*");
									panel(new PanelBuilder("playerBox") {{
										childLayoutVertical();
									}});
								}});
							}});
						}});
						panel(new PanelBuilder() {{	
							width("21px");
						}});
					}});
					panel(new PanelBuilder() {{	
						childLayoutHorizontal();
						panel(new PanelBuilder() {{	
							width("21px");
						}});
						panel(new PanelBuilder() {{	
							childLayoutVertical();
							panel(new PanelBuilder() {{
								height("21px");
							}});
							panel(new PanelBuilder() {{
								childLayoutHorizontal();
								panel(new PanelBuilder() {{
									
								}});
								control(new ButtonBuilder("startKIButton", "KI Join Game") {{
									alignCenter();
									valignCenter();
									interactOnClick("kIJoin()");
									width("150px");
								}});
								panel(new PanelBuilder() {{
									width("21px");
								}});
								control(new ButtonBuilder("joinGameButton", "Join Game") {{
									alignCenter();
									valignCenter();
									interactOnClick("joinGame()");
									width("150px");
								}});
								panel(new PanelBuilder() {{
									width("21px");
								}});
								control(new ButtonBuilder("createGameButton", "Create Game") {{
									alignCenter();
									valignCenter();
									interactOnClick("createGame()");
									width("150px");
								}});
							}});
							panel(new PanelBuilder() {{
								height("21px");
							}});
							text(new TextBuilder() {{
								font("aurulent-sans-36-bold-with-shadow.fnt");
								color(new Color(1, 1, 1, 1));
								text("Chat");
							}});
							control(new ListBoxBuilder("chatBox") {{
								style("nifty-listbox");
								displayItems((768 / 64) - 2);
								selectionModeDisabled();
								hideHorizontalScrollbar();
								optionalVerticalScrollbar();
							}});
							panel(new PanelBuilder() {{
								height("21px");
							}});
							panel(new PanelBuilder() {{
								childLayoutHorizontal();
								control(new TextFieldBuilder("chatMessagesField", "") {{ 
									alignCenter();
									valignCenter();
									maxLength(255);
								}});
								panel(new PanelBuilder() {{
									width("21px");
								}});
								control(new ButtonBuilder("sendButton", "Send") {{
									alignCenter();
									valignCenter();
									interactOnClick("sendMessage()");
									width("100px");
								}});
							}});
							panel(new PanelBuilder() {{
								height("21px");
							}});
						}});
						panel(new PanelBuilder() {{	
							width("21px");
						}});
						panel(new PanelBuilder() {{	
							width("389px");
							childLayoutVertical();
							panel(new PanelBuilder() {{
								height("21px");
							}});
							image(new ImageBuilder() {{
									valignCenter();
									alignCenter();
									height("222px");
									filename("res/img/logo/wasp_edited.png");
							}});
							panel(new PanelBuilder() {{
								height("21px");
							}});
							text(new TextBuilder() {{
								font("aurulent-sans-36-bold-with-shadow.fnt");
								color(new Color(1, 1, 1, 1));
								text("Creep Smash");
								valignCenter();
								alignCenter();
							}});
							panel(new PanelBuilder() {{
								height("21px");
							}});
							control(new ButtonBuilder("logoutButton", "Logout") {{
								width("100px");
								alignRight();
								valignCenter();
								visibleToMouse(true);
								interactOnClick("logout()");
							}});
							panel(new PanelBuilder() {{
								height("21px");
							}});
						}});
						panel(new PanelBuilder() {{	
							width("21px");
						}});
					}});
				}});
			}});
		}}.build(gui.getNifty());
		
		gui.getNifty().addScreen("lobby", screen);
	}
	
	
	@Override
	public void render(float timeElapsed) {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		
		if(previewImage != null) {
			NiftyImage img = gui.getNifty().getRenderEngine().createImage(previewImage.getAbsolutePath(), false);
			Element e = gui.getNifty().getScreen("lobby").findElementByName("map_preview");
			if(e != null) {
				e.getRenderer(ImageRenderer.class).setImage(img);
			}
			
			previewImage = null;
		}
		
		if (lobbyController.getErrorOccured()) {
			gui.getLobbyScreen().getLobbyController().showErrorPopup();
			lobbyController.setErrorOccured(false);
		}
	}
	
	public String getFontName() {
		return fontName;
	}

	@Override
	public void init() {}

	@Override
	public void keyPressed(int key) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(int key) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(int posX, int posY, int button) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(int posX, int posY, int button) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(int posX, int posY, int button) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(int posX, int posY, int button) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseWheelMoved(int movement) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dealloc() {
		if(lobbyController.getRefreshLobby() != null) {
			lobbyController.getRefreshLobby().shutdown();
		}
	}

	@Override
	public void eventOccured(Event event) {
		if (event.getKey().equals("chat")) {
			lobbyController.fillChatBox(event.getValue(), event.getColor());
		} else if (event.getKey().equals("player")) {
			lobbyController.checkForUpdatePlayerInTeamList(event.getPlayers());
		} else if (event.getKey().equals("games")) {
			lobbyController.checkForUpdateGameList(event.getGames());
		} else if(event.getKey().equals("gameAlreadyExists")) {
			System.out.println("game already exists");
		} else if(event.getKey().equals("temporaryUser")) {
			if(lobbyController.getCreateGameScreen().getCreateGame().isVisible()) {
				String[] split = event.getValue().split(" ");
				String[] name = split[2].split("=");
				String[] password = split[3].split("=");
				String gameName = gui.getStart().getModel().getGameName();
				boolean view = gui.getStart().getModel().getView(); 
				lobbyController.getCreateGameController().createBot(name[1], password[1], gameName, view);
			}
		}
	}
	
	public LobbyController getLobbyController() {
		return lobbyController;
	}
	
	public void setPreviewImage(File previewImage) {
		this.previewImage = previewImage;
	}
	
	public boolean canPoll() {
		return true;
	}
	
	@Override
	public String getNiftyScreenID() {
		return "lobby";
	}
}
